var QualityAssurancesByone = {//质量管理管理初始化
    id: "QualityAssurancesOneTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

function _cellStyles(value, row, index) {
    if (row.status == 1) {
        return {css: {"background-color": "#fff1d5", "overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    } else {
        return {css: {"overflow": "hidden", "white-space": "nowrap", "text-overflow": "ellipsis"}}
    }
}

QualityAssurancesByone.initColumn = function () {//初始化表格的列
    return [
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'reservationsId', field: 'reservationsId', visible: false, align: 'center', valign: 'middle'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle'},
        {title: 'num', field: 'num', visible: false, align: 'center', valign: 'middle'},
        {title: 'status', field: 'status', visible: false, align: 'center', valign: 'middle'},
        {title: 'iconName', field: 'iconName', visible: false, align: 'center', valign: 'middle'},
        {title: 'standardScore', field: 'standardScore', visible: false, align: 'center', valign: 'middle'},
        {
            title: '上传时间', field: 'addTime', align: 'center', valign: 'middle', sortable: true, width: 120,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '上传人姓名', field: 'addName', align: 'center', valign: 'middle', sortable: true, width: 70,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '门诊', field: 'outpatientName', align: 'center', valign: 'middle', sortable: true, width: 70,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '牙位', field: 'toothPosition', align: 'center', valign: 'middle', sortable: true, width: 70,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 70,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '类型', field: 'iconName', align: 'center', valign: 'middle', sortable: true, width: 70,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '图片', field: 'imageUrl', align: 'center', valign: 'middle', sortable: true, width: 120,
            formatter: function (value, row, index) {
                return "<img src='" + value +"?temp=" + Math.random()+ "' class='imagePath' style='width: 80px;height: 40px;'>"
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '分数', field: 'score', align: 'center', valign: 'middle', sortable: true, width: 70,
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '总分', field: 'scoreTOTAL', align: 'center', valign: 'middle', sortable: true, width: 70,
            formatter: function (value, row, index) {
                if (value != null && value != '') {
                    if (value >= 0) {
                        return parseFloat(value).toFixed(2);
                    } else {
                        return 0;
                    }
                } else {
                    return "";
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '是否合格', field: 'isQualified', align: 'center', valign: 'middle', sortable: true, width: 70,
            formatter: function (value, row, index) {
                if (row.scoreTOTAL >= row.standardScore) {
                    return "<span>合格</span>";
                } else if (row.scoreTOTAL > 0 && row.scoreTOTAL < row.standardScore) {
                    return "<span>不合格</span>";
                } else {
                    return "";
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        },
        {
            title: '操作', field: 'operation', align: 'center', valign: 'middle', width: 60,
            formatter: function (value, row, index) {
                var num = row.num;
                var ids = row.num + "," + row.reservationsId + "," + $("#customerId").val();
                if (row.status == 1) {
                    if (row.iconName == "术中") {
                        // return '<button title="详情" onclick="QualityAssurancesByone.detailsed(' + "'" + num + "'" + ')" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0 5px;" type="button" class="btn"><span class="glyphicon glyphicon-pencil"></span></button>';
                    } else {
                        return "";
                    }
                } else if (row.status == 2) {
                    if (row.iconName == "术中") {
                        // return '<button title="详情" onclick="QualityAssurancesByone.openUpdateIconTypes(' + "'" + ids + "'" + ')" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0 5px;" type="button" class="btn"><span class="glyphicon glyphicon-pencil"></span></button>' +
                        return '<button title="删除" onclick="QualityAssurancesByone.deleteScoringIcons(' + "'" + num + "'" + ')" type="button" class="btn" style="background: #ffffff;border: 1px solid #cccccc;padding: 1px 4px 0px 5px;"><span class="glyphicon glyphicon-trash"></span></button>';
                    } else {
                        return "";
                    }
                } else {
                    return "";
                }
            },
            cellStyle: function (value, row, index) {
                return _cellStyles(value, row, index);
            }
        }
    ];
};

QualityAssurancesByone.openUpdateIconTypes = function (ids) {//打开update影像
    var index = layer.open({
        type: 2,
        title: '请关联预约',
        area: ['60%', '70%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/doctorLooked/openUpdateIconTypes/' + ids
    });
    this.layerIndex = index;
};

QualityAssurancesByone.deleteScoringIcons = function (num) {//删除整组打分影像
    if (confirm("是否删除此组图片？")) {
        $.ajax({
            url: Feng.ctxPath + "/doctorLooked/deleteScoringIcons",
            type: "post",
            data: {
                num: num
            },
            success: function (data) {
                Feng.success("删除成功!");
                QualityAssurancesByone.table.refresh();
            },
        });
    }
};

mergeCells = function (data, fieldName, colspan, target) {
    var sort = new Array();
    var num = 1;
    for (var i = 0; i < data.length - 1; i++) {
        for (var prop in data[i]) {
            if (prop == fieldName) {
                if (data[i][prop] == data[i + 1][prop]) {
                    num++;
                    if (i == data.length - 2) {
                        sort.push(num)
                    }
                } else {
                    sort.push(num);
                    num = 1
                }
                break
            }
        }
    }
    var index = 0;
    for (var j = 0; j < sort.length; j++) {
        var count = sort[j];
        $(target).bootstrapTable('mergeCells', {
            index: index,
            field: fieldName,
            colspan: colspan,
            rowspan: count
        });
        index += count
    }
};

QualityAssurancesByone.merge = function () {
    var data = $('#QualityAssurancesTable').bootstrapTable('getData', true);
    mergeCells(data, "addTime", 1, $('#QualityAssurancesTable'));
};

QualityAssurancesByone.detailsed = function (num) {//点击表格展示详情
    var index = layer.open({
        type: 2,
        title: '详情',
        area: ['80%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/qualityAssurances/score_detailsed/' + num
    });
    this.layerIndex = index;
};

QualityAssurancesByone.details = function (num) {//点击表格展示详情
    var index = layer.open({
        type: 2,
        title: '详情',
        area: ['80%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/qualityAssurances/score_details/' + num
    });
    this.layerIndex = index;
};

QualityAssurancesByone.check = function () { // 检查是否选中
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        QualityAssurancesByone.seItem = selected[0];
        return true;
    }
};

QualityAssurancesByone.search = function () {//
    QualityAssurancesByone.table.refresh({query: QualityAssurancesByone.formParams()});
};

QualityAssurancesByone.inquire = function () {//人数统计 + 表格刷新
    QualityAssurancesByone.search();
};

QualityAssurancesByone.formParams = function () {//查询质量管理列表
    var queryData = {};
    queryData['reservationsId'] = $("#reservationsId").val();
    return queryData;
};

$(function () {

});
